package com.bawei.lib_storage.database.dao

import androidx.room.*
import com.bawei.lib_storage.database.entity.HistoryVideoEntity

@Dao
interface HistoryVideoDao {
    
    @Query("SELECT * FROM b_history_video WHERE userId = :userId ORDER BY watchTime DESC")
    fun getAllByUserId(userId: String): List<HistoryVideoEntity>
    
    @Query("SELECT * FROM b_history_video WHERE videoId = :videoId AND userId = :userId LIMIT 1")
    fun getByVideoIdAndUserId(videoId: Int, userId: String): HistoryVideoEntity?
    
    @Query("SELECT EXISTS(SELECT 1 FROM b_history_video WHERE videoId = :videoId AND userId = :userId)")
    fun isInHistory(videoId: Int, userId: String): Boolean
    
    @Insert
    fun insert(historyVideo: HistoryVideoEntity)
    
    @Update
    fun update(historyVideo: HistoryVideoEntity)
    
    @Delete
    fun delete(historyVideo: HistoryVideoEntity)
    
    @Query("DELETE FROM b_history_video WHERE videoId = :videoId AND userId = :userId")
    fun deleteByVideoIdAndUserId(videoId: Int, userId: String)
    
    @Query("DELETE FROM b_history_video WHERE userId = :userId")
    fun deleteAllByUserId(userId: String)
    
    @Query("DELETE FROM b_history_video WHERE userId = :userId AND watchTime < :timeThreshold")
    fun deleteOldHistory(userId: String, timeThreshold: Long)
} 